"""
Case Type   : resource pool
Case Name   : 普通资源池修改为组资源池，合理报错
Create At   : 2022/08/29
Owner       : songjing20
Description :
    1.预置子class控制组及Workload控制组
    2.创建普通资源池关联workload_a1控制组
    3.修改控制组为上级子class控制组class_a
    4.修改控制组为子class控制组class_b
    5.环境清理
Expect      :
    1.创建成功
    2.创建成功
    3.修改失败
    4.修改失败
    5.清理成功
History     :
"""
import os
import unittest

from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class ResourcePool(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.pri_node = Node('PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.class_name1 = 'class_a'
        self.class_name2 = 'class_b'
        self.group_name = 'workload_a1'
        self.respool = 'resp_resource_pool_0048'

    def test_resource_pool(self):
        text = "-----step1:预置子class控制组及Workload控制组;expect:创建成功-----"
        self.log.info(text)
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gs_cgroup -c -S {self.class_name1} -s 20; " \
              f"gs_cgroup -c -S {self.class_name1} -G{self.group_name} -g 20" \
              f"; gs_cgroup -c -S {self.class_name2} -s 10; " \
              f"gs_cgroup -p"
        self.log.info(cmd)
        cmd_res = self.pri_node.sh(cmd).result()
        self.log.info(cmd_res)
        self.assertIn(f"CLASS  Name: {self.class_name1}", cmd_res,
                      f'执行失败:{text}')
        self.assertIn(f"CLASS  Name: {self.class_name2}", cmd_res,
                      f'执行失败:{text}')
        self.assertIn(f"DEFWD  Name: {self.group_name}", cmd_res,
                      f'执行失败:{text}')

        text = "-----step2:创建普通资源池关联workload_a1控制组;expect:创建成功-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            f"create resource pool {self.respool} with(control_group = "
            f"'{self.class_name1}:{self.group_name}');")
        self.log.info(sql_res)
        self.assertEqual(self.constant.create_resource_pool_success, sql_res,
                         f'执行失败:{text}')

        text = "-----step3:修改控制组为上级子class控制组class_a;expect:创建失败，合理报错-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            f"alter resource pool {self.respool} "
            f"with(control_group = '{self.class_name1}');")
        self.log.info(sql_res)
        flag = 'ERROR:  cannot alter normal resource pool to the ' \
               'two-layer resource pool list.'
        self.assertIn(flag, sql_res, f'执行失败:{text}')

        text = "-----step4:修改控制组为上级子class控制组class_b;expect:创建失败，合理报错-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            f"alter resource pool {self.respool} "
            f"with(control_group = '{self.class_name2}');")
        self.log.info(sql_res)
        self.assertIn(flag, sql_res, f'执行失败:{text}')

    def tearDown(self):
        text = "-----step4:环境清理;expect:成功-----"
        self.log.info(text)
        drop_res = self.pri_sh.execut_db_sql(
            f"drop resource pool if exists {self.respool}")
        self.log.info(drop_res)
        cmd = f"source {macro.DB_ENV_PATH};gs_cgroup --revert"
        self.log.info(cmd)
        cmd_res = self.pri_node.sh(cmd).result()
        self.assertIn(self.constant.drop_resource_pool_success, drop_res,
                      f'执行失败:{text}')
        self.assertEqual('', cmd_res, f'执行失败:{text}')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
